package co.fitstart.weapp.api.web.controller.coach;

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.entity.principal.Principal;
import co.fitstart.weapp.api.entity.RestOperationRequest;
import co.fitstart.weapp.api.service.CoachScheduleService;
import co.fitstart.weapp.api.service.CoachService;

/**
 * 教练-班表
 * @author haiyan_xiao
 *
 */
@RestController
@RequestMapping("/coach/schedules")
public class CoachScheduleController {

    private static final Logger logger = LoggerFactory.getLogger(CoachScheduleController.class);

    @Autowired
    private CoachService coachService;

    @Autowired
    private CoachScheduleService coachScheduleService;

    /**
     * 
     * @param mobile
     * @param day
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult list(@RequestParam("mobile") String mobile,
                           @RequestParam(value = "day", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date day) {
        HttpResult httpResult = new HttpResult();
        
        try {
            Principal coach = coachService.load(mobile);
            Date today = DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
            
            if(day == null) {
                day = today;
            }
            
            httpResult.success().message("获取数据成功")
                                .data(coachScheduleService.getTimeBanner(day))
                                .data("editable", day.before(today) ? false : true) //是否可编辑
                                .data("businessTimes", coachScheduleService.listBusinessTime(coach, day)) //工作时间
                                .data("schedules", coachScheduleService.list(coach, day)); //班表
        } catch (Exception e) {
            logger.error("#CoachScheduleController# list() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }

    /**
     * 
     * @param mobile
     * @param day
     * @param type
     * @return
     */
    @RequestMapping(value = "/rest", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult rest(@RequestParam("mobile") String mobile,
                           @RequestParam("day") @DateTimeFormat(pattern = "yyyy-MM-dd") Date day,
                           @RequestParam("type") String type) {
        HttpResult httpResult = new HttpResult();
        
        try {
            Principal coach = coachService.load(mobile);
            
            httpResult.success().message("获取数据成功")
                                .data(coachScheduleService.getTimeBanner(day)) //下一周周一
                                .data("businessTimes", coachScheduleService.listBusinessTimes(coach, day, type));
        } catch (Exception e) {
            logger.error("#CoachScheduleController# rest() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }

    /**
     * 添加/删除休息
     * @param mobile
     * @param request
     * @return
     */
    @RequestMapping(value = "/rest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult onRest(@RequestParam("mobile") String mobile,
                             @RequestBody RestOperationRequest request) {
        HttpResult httpResult = new HttpResult();
        
        try {
            Principal coach = coachService.load(mobile);
            
            coachScheduleService.rest(coach, request.getType(), request.getDay(), request.getTimes());
            
            httpResult.success().message("add".equals(request.getType()) ? "添加休息成功" : "删除休息成功");
        } catch (BusinessException e) {
            httpResult.fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("#CoachScheduleController# onRest() error", e);
            httpResult.fail("500").message("操作失败");
        }
        
        return httpResult;
    }

    /**
     * 清空
     * @param mobile
     * @param request
     * @return
     */
    @RequestMapping(value = "/reset", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult reset(@RequestParam("mobile") String mobile,
                            @RequestParam("day") @DateTimeFormat(pattern = "yyyy-MM-dd") Date day) {
        HttpResult httpResult = new HttpResult();
        
        try {
            Principal coach = coachService.load(mobile);
            
            coachScheduleService.reset(coach, day);
            
            httpResult.success().message("清空休息成功");
        } catch (Exception e) {
            logger.error("#CoachScheduleController# reset() error", e);
            httpResult.fail("500").message("清空休息失败");
        }
        
        return httpResult;
    }

    /**
     * 复制上一周
     * @param mobile
     * @param day
     * @return
     */
    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult copy(@RequestParam("mobile") String mobile,
                           @RequestParam("day") @DateTimeFormat(pattern = "yyyy-MM-dd") Date day) {
        HttpResult httpResult = new HttpResult();
        
        try {
            Principal coach = coachService.load(mobile);
            
            coachScheduleService.copy(coach, day);
            
            httpResult.success().message("复制上一周成功");
        } catch (BusinessException e) {
            httpResult.fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("#CoachScheduleController# copy() error", e);
            httpResult.fail("500").message("复制上一周失败");
        }
        
        return httpResult;
    }
}
